<?php
/**
 * Title: Timezone Support
 * Description: Adds timezone support for Concerto. This module lets you assign timezones to specific contents and/or allow users to choose their timezone. Current time can be set either according to the content currently being browsed (or its ancestors, if applicable), or by the user's selected timezone. Current time is accessed by using the global variable $cn_current_time, which is an instance of the PHP DateTime object.
 * Author: Jason Raede
 * Version: 1.0
 */




Modulator()->registerFunction('admin_sidebar', 'cn_tz_sidebar');
Modulator()->registerFilter('form_build_input', 'cn_tz_build_input');
Modulator()->registerFunction('form_display_input', 'cn_tz_display_input');
Modulator()->registerFilter('form_validate_data', 'cn_tz_validate_data');
Modulator()->registerFilter('feedback_timestamp_display', 'cn_tz_adjust_timestamp');
Modulator()->registerPanel('timezone_support', CN_APPLICATION_PATH.'modules/cn-timezone-support/panel.php');
Modulator()->registerFunction('url_interpret_end', 'cn_tz_init_variables');

/** Set the global time variable */
global $cn_current_time;
global $cn_tz_name;

function cn_tz_init_variables() {
	global $cndb;
	global $cn_current_user;
	global $cn_url_info;
	global $cn_current_time;
	global $cn_tz_name;
	$tz_by = cn_get_option('cn_tz_by');
	$tz_listing = cn_get_option('cn_tz_listing');
	$tz_default = cn_get_option('cn_tz_default');
	
	if($tz_by == 'user') {
		/** If user is logged in, grab their time zone. Otherwise, use default time zone */
		if($cn_current_user->timezone) {
			$timezone = $cn_current_user->timezone;
		}
	}
	elseif($tz_by == 'listing') {
		/** Grab the current listing (or ancestral listing) of that listing type */
		if(count($cn_url_info)) {
			foreach($cn_url_info as $info) {
				if($info['type'] == $tz_listing) {
					$timezone = $cndb->get_var("SELECT `l".$tz_listing."_timezone` FROM `cn_listing_type$tz_listing` WHERE `l".$tz_listing."_id`='".$info['id']."'");
					break;
				}
			}
		
		
		}
	}
	elseif($tz_by == 'custom') {
		if($_SESSION['user_timezone']) {
			$timezone = $_SESSION['user_timezone'];
		}
	}
	
	if(!$timezone) $timezone = $tz_default;
	
	$cn_tz_name = $cndb->get_var("SELECT `tz_name` FROM `cn_timezones` WHERE `tz_id`='$timezone'");
	
	if($cn_tz_name) $cn_current_time = new DateTime('now', new DateTimeZone($cn_tz_name));


}

function cn_tz_adjust_timestamp($timestamp) {
	global $cn_tz_name;
	$time = new DateTime($timestamp);
	if($cn_tz_name) $time->setTimezone(new DateTimeZone($cn_tz_name));

	return $time->format('m/d/Y H:i:s');
}



function cn_tz_validate_data($content, $args) {
	$field = $args['field'];
	
	if($field->lf_data_type == 'Timezone') {
		
		$content['db_fields'][$field->lf_column] = $_REQUEST[$field->lf_column];
	}
	
	return $content;
}

function cn_current_time($format = 'g:ia') {
	global $cn_current_time;
	echo $cn_current_time->format($format);
}

function cn_tz_display_input($args) {
	$form_elements = $args;
	if($form_elements['data_type'] == 'Timezone') :?>
	<fieldset>
		<legend><?=$form_elements['display']?></legend>
		<?=$form_elements['elements']['element']?>
	
	</fieldset>
	
	<?php endif;
}

function cn_tz_build_input($content, $args) {
	global $cndb;
	$listing = $args['listing'];
	$field = $args['field'];
	if($field->lf_data_type == 'Timezone') {
		/** User or listing? */
		if($field->lf_ltype == '0') {
			$value = $cndb->get_var("SELECT `user_timezone` FROM `cn_users` WHERE `user_id`='$listing'");
		}
		else {
			$table = 'cn_listing_type'.$field->lf_ltype;
			$value = $cndb->get_var("SELECT `$field->lf_column` FROM `$table` WHERE `l".$field->lf_ltype."_id` = '$listing'");
		}
		$column = $field->lf_column;
		
		$form_elements = array('column'=>$column, 'display'=>$field->lf_display_name, 'data_type'=>$field->lf_data_type, 'elements'=>array());
		
		$allowed = cn_get_option('cn_tz_allow');
		$allowed_timezones = array();
		if($allowed) $allowed_timezones = unserialize($allowed);
		
		$allowed_timezones = "`tz_name` LIKE '".implode("%' OR `tz_name` LIKE '", $allowed_timezones)."%'";
		
		$results = $cndb->get_results("SELECT * FROM `cn_timezones` WHERE $allowed_timezones ORDER BY `tz_name`");
		$elements = '<select name="'.$column.'">';
		foreach($results as $result) {
			if($result->tz_id == $value) $elements .= '<option selected="selected" value="'.$result->tz_id.'">'.$result->tz_name.'</option>';
			else $elements .= '<option value="'.$result->tz_id.'">'.$result->tz_name.'</option>';
		}
		$elements .= '</select>';
		$form_elements['elements']['element'] = $elements;
	}
	
	return $form_elements;
	
}

function cn_tz_sidebar() {
	?>
	<li><a href="?display=timezone_support"><span>Timezone Support</span></a></li>
	<?php
}

?>